package controllers

import (
	"fmt"
	"log"
	m "xwaf_admin/models"

	//"strconv"
	"strings"

	"github.com/tidwall/gjson"
	//"golang.org/x/text/encoding/simplifiedchinese"
)

type WafconfigController struct {
	CommonController
}

func (this *WafconfigController) GetConfig() {
	appname := this.GetString("appname")
	if appname == "" {
		this.Ctx.WriteString(`{"code":-1,"msg":"NeedAppname"}`)
		return
	}

	AuthToken := this.Ctx.Request.Header.Get("Auth-Token")
	if AuthToken != ApiToken {
		this.Data["json"] = &map[string]interface{}{"code": -1, "msg": "ApiTokenFail", "count": 0}
		this.ServeJSON()
		return
	}

	var stu m.Xf_waf_config
	stu = m.GetConfigAppname(appname)
	stu.Waf_html = strings.Replace(stu.Waf_html, "\\", "", -1)
	stu.Limit_html = strings.Replace(stu.Limit_html, "\\", "", -1)
	this.Data["json"] = stu
	this.ServeJSON()
}

func (this *WafconfigController) GetWafConfig() {
	// userinfo := this.GetSession("userinfo")
	// if userinfo == nil {
	// 	//this.Ctx.Redirect(302, "/public/Ulogin")
	// 	this.Data["json"] = &map[string]interface{}{"code": 201, "msg": "登录过期"}
	// 	this.ServeJSON()
	// 	return
	// }
	appname := this.GetString("appname")
	if appname == "" {
		this.Ctx.WriteString(`{"code":-1,"msg":"NeedAppname"}`)
		return
	}
	var stu m.Wafconfig
	stu = m.GetWafConfigByAppname(appname)
	this.Data["json"] = stu
	this.ServeJSON()
}

func (this *WafconfigController) GetReqlimitConfig() {
	appname := this.GetString("appname")
	if appname == "" {
		this.Ctx.WriteString(`{"code":-1,"msg":"NeedAppname"}`)
		return
	}
	var stu m.Reqlimitconfig
	stu = m.GetReqLimitConfigByAppname(appname)
	this.Data["json"] = stu
	this.ServeJSON()
}

func (this *WafconfigController) GetOtherConfig() {
	appname := this.GetString("appname")
	if appname == "" {
		this.Ctx.WriteString(`{"code":-1,"msg":"NeedAppname"}`)
		return
	}
	var stu m.Otherconfig
	stu = m.GetOtherConfigByAppname(appname)
	this.Data["json"] = stu
	this.ServeJSON()
}

func (this *WafconfigController) Version() {
	appname := this.GetString("appname")
	if appname == "" {
		this.Ctx.WriteString(`{"code":-1,"msg":"NeedAppname"}`)
		return
	}
	AuthToken := this.Ctx.Request.Header.Get("Auth-Token")
	if AuthToken != ApiToken {
		this.Data["json"] = &map[string]interface{}{"code": -1, "msg": "ApiTokenFail", "count": 0}
		this.ServeJSON()
		return
	}

	vv := m.GetConfigVersionByAppname(appname)
	this.Data["json"] = &map[string]interface{}{"config_version": vv}
	this.ServeJSON()
}

func (this *WafconfigController) EditWafConfig() {
	appname := this.GetString("appname")
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)

	if gp.Get("name").Exists() {
		name := gp.Get("name").String()
		value := gp.Get("value").String()
		_, err := m.EditWafConfigByAppname(appname, name, value)
		if err == nil {
			this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
		} else {
			log.Printf("[ERROR] update waf config Fail:%s\n", err)
			this.Ctx.WriteString(fmt.Sprintf(`{"code":-1,"msg":"%s"}`, err.Error()))
		}
	} else {
		this.Ctx.WriteString(`{"code":-1,"msg":"bodyParseFail"}`)
	}
}

/*
func (this *WafconfigController) UpdateLimitHtml() {
	appname := this.GetString("appname")
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)
	limitcode := gp.Get("reqlimit_statuscode").String()
	limithtml := gp.Get("limit_html").String()
	_, err := m.UpdateLimitHtmlByAppname(appname, limitcode, limithtml)
	if err == nil {
		this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
	} else {
		log.Printf("[ERROR] update wafconfig limithtml Fail:%s\n", err)
		this.Ctx.WriteString(fmt.Sprintf(`{"code":-1,"msg":"%s"}`, err.Error()))
	}

}

func (this *WafconfigController) UpdateWafModel() {
	appname := this.GetString("appname")
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)
	wafmodel := gp.Get("waf_model").String()
	wafhtml := gp.Get("waf_html").String()
	wafRUrl := gp.Get("waf_redirect_url").String()
	wafETime := gp.Get("waf_expire_time").String()
	urlLength := gp.Get("waf_url_length").String()
	_, err := m.UpdateWafModelByAppname(appname, wafmodel, wafhtml, wafRUrl, wafETime, urlLength)
	if err == nil {
		this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
	} else {
		log.Printf("[ERROR] update wafconfig wafhtml Fail:%s\n", err)
		this.Ctx.WriteString(fmt.Sprintf(`{"code":-1,"msg":"%s"}`, err.Error()))
	}

}
*/
func (this *WafconfigController) Update() {
	appname := this.GetString("appname")
	this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
	this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json;charset=utf-8")
	gp := gjson.ParseBytes(this.Ctx.Input.RequestBody)

	logmodel := gp.Get("log_model").String()
	kfkip := gp.Get("kafka_broker_ip").String()
	waf_riskip_ratev := gp.Get("waf_riskip_ratev").String()
	waf_riskip_model := gp.Get("waf_riskip_model").String()
	limitcode := gp.Get("reqlimit_statuscode").String()
	limithtml := gp.Get("limit_html").String()

	wafmodel := gp.Get("waf_model").String()
	wafhtml := gp.Get("waf_html").String()
	wafRUrl := gp.Get("waf_redirect_url").String()
	wafETime := gp.Get("waf_expire_time").String()
	urlLength := gp.Get("waf_url_length").String()
	excludeCookie := gp.Get("exclude_cookie").String()
	_, err := m.UpdateWafConfig(appname, logmodel, kfkip, waf_riskip_model, waf_riskip_ratev, limitcode, limithtml, wafmodel, wafhtml, wafRUrl, wafETime, urlLength, excludeCookie)
	if err == nil {
		this.Ctx.WriteString(`{"code":0,"msg":"success"}`)
	} else {
		log.Printf("[ERROR] update wafconfig wafhtml Fail:%s\n", err)
		this.Ctx.WriteString(fmt.Sprintf(`{"code":-1,"msg":"%s"}`, err.Error()))
	}

}
